package cn.usian.service.impl;

import cn.usian.jyzx.model.entity.product.Category;
import cn.usian.mapper.CategoryMapper;
import cn.usian.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceimpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<Category> findCategoryTree() {
        List<Category> all = categoryMapper.findAll();
        List<Category> onelever = all.stream().filter(one -> one.getParentId().equals(0l)).collect(Collectors.toList());
        onelever.stream().forEach(one->{
            List<Category> towlever = all.stream().filter(category -> category.getParentId().equals(one.getId())).collect(Collectors.toList());
            towlever.stream().forEach(tow->{
                List<Category> three = all.stream().filter(category -> category.getParentId().equals(tow.getId())).collect(Collectors.toList());
                tow.setChildren(three);
            });
            one.setChildren(towlever);
        });

        return onelever;
    }

//    @Override
//    public List<Category> findCategoryTree() {
//
//        List<Category> oneLever=categoryMapper.findC();
//
//        for (Category one : oneLever) {
//           List<Category> towLever= categoryMapper.findTow(one.getId());
//            for (Category tow : towLever) {
//                List<Category> threeLever=categoryMapper.findThree(tow.getId());
//                tow.setChildren(threeLever);
//            }
//           one.setChildren(towLever);
//        }
//        return oneLever;
//    }
}
